#!/usr/bin/env python2.7
# -*- conding: utf-8 -*-

''' Unit Test
'''

import sys
sys.path.append('../src')
import stock
import trade
from tradermanager import TraderManager

import time
from unittest import TestCase
from unittest import TestSuite
from unittest import TestResult
from unittest import TextTestRunner

class StockTest(TestCase):
    def setUp(self):
        self.stock = stock.StockBase(name='', price=25.0, quantity=1e6)
        self.name = 'Test Stock'
        self.seq = range(10)

    def test_name(self):
        print "Stock: ", self.stock
 
class TradeReauestTest(TestCase):
    def setUp(self):
        self.request = trade.TradeRequest(trade.Trader(), stock.StockBase())

    def test_name(self):
        print "TradeRequest: ", self.request

class TraderTest(TestCase):
    def setUp(self):
        self.trader = trade.Trader()

    def test_name(self):
        print("%s's name is %s" % (self.trader, self.trader.name))
        self.assertTrue(self.trader.name == 'Trader')

    def test_uuid(self):
        print("%s's trader ID is %s" % (self.trader, self.trader.tid))

    def test_run(self):
        self.trader.start()
        time.sleep(3)
        self.trader.stop()

class TraderManagerTest(TestCase):
    def setUp(self):
        self.tm = TraderManager()

    def test_name(self):
        #element = random.choice(self.seq)
        self.assertTrue(self.tm.name == 'Trader Manager')

    def test_db(self):
        r = self.tm.init_db()
        self.assertTrue(r)

class StockExchangeTest(TestCase):
    def setUp(self):
        self.stock_exchange = None

    def test_name(self):
        #element = random.choice(self.seq)
        self.assertTrue(self.name == 'Test Stock')

class SequenceFunctionsTest(TestCase):
    def setUp(self):
        self.seq = range(10)

    def test_shuffle(self):
        # make sure the shuffled sequence does not lose any elements
        random.shuffle(self.seq)
        self.seq.sort()
        self.assertEqual(self.seq, range(10))

        # should raise an exception for an immutable sequence
        self.assertRaises(TypeError, random.shuffle, (1,2,3))

    def test_choice(self):
        element = random.choice(self.seq)
        self.assertTrue(element in self.seq)

    def test_sample(self):
        with self.assertRaises(ValueError):
            random.sample(self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assertTrue(element in self.seq)

if __name__ == '__main__':
    ts = TestSuite()
    ts.addTest(TraderTest('test_name'))
    ts.addTest(TraderTest('test_uuid'))
    ts.addTest(TraderTest('test_run'))

    ts.addTest(StockTest('test_name'))
    ts.addTest(TradeReauestTest('test_name'))
 
    TextTestRunner(verbosity=2).run(ts)

